﻿    using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace EmotionRecognitionDB
{//
    /// <summary>
    /// Connects with the predefined SQL database - Used as intermidiator between DataBase and code
    /// </summary>
    public  class DBConnection : IDisposable
    {
        private static string myPassword = "1987";      // machine based mySql password
        private static DBConnection instance = null; //DBconnection singelton
        private MySqlConnection connection = null; //holds connection
        public MySqlCommand command = null;    //holds query
        public MySqlDataReader reader = null;   //holds data read from SQL
        public MySqlDataAdapter adapter = null;    //an intermediary between the DataSet and the data source

        /// <summary>
        /// Establish connection to the DataBase
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
        private  DBConnection()
        {
            string MyConString = "Server=127.0.0.1;Port=3306;Database=FinalProject;Uid=root;password=" + myPassword + ";"; //connection string
            connection = new MySqlConnection(MyConString);
            command = new MySqlCommand();
            adapter = new MySqlDataAdapter(command.CommandText,connection);
            command.Connection = connection;
            
          
            try
            {
                connection.Open();
            }
            catch (MySqlException ex)
            {
                MessageBox.Show("Error: " + ex.ToString());

            }
            DBQueries.CreatePathMatcher();
        }
        /// <summary>
        /// Establish connection to the DataBase using singelton
        /// </summary>

        public static DBConnection Instance
        {
            get
            {
            if (instance == null)
                {
                    instance = new DBConnection();
                }
            return instance;
        }       
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // dispose managed resources
                connection.Close();
                command.Dispose();
                reader.Close();
                adapter.Dispose();
            }
            // free native resources
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }


        internal void UpdateAdapter()
        {
            adapter = new MySqlDataAdapter(command.CommandText, connection);
        }
    }
}
//jZ5sX8zy8EH7 google code pass
//root , 1234 for server